/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012 Oliver Katz */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		printf("usage: %s <INPUT>\n\n");
		printf("Builds a display engine plugin (*.so) from an\n");
		printf("input source file (*.cpp)\n");
		return 1;
	}

	char *input = argv[1];

	char *base = strdup(argv[1]);
	*(strchr(base, '.')) = 0;

	char soname[64];
	sprintf(soname, "lib%s.so", base);

	char ldargs[128];
	sprintf(ldargs, "-Wl,-soname,%s", soname);

	char *args[] = {
		"g++",
		"-std=c++11",
		"-fPIC",
		"-shared",
		ldargs,
		input,
		"-o",
		soname,
		"-L.",
		"-l-_-display-engine",
		NULL
	};

	if (execvp(args[0], args) != 0)
	{
		printf("error: cannot execute: g++ -std=c++11 -shared %s %s -o %s\n", ldargs, input, soname);
		printf("error: errno: %s\n", strerror(errno));
		return 1;
	}
	return 0;
}